<?php

namespace app\common\model;

use think\Model;

/**
 * 消息模块
 */
class Notice extends Model
{

    // 表名
    protected $name = 'notice';

    /**
     * 创建通知记录的公共方法
     * @param array $data 通知数据
     * @return bool|int 成功返回插入的ID，失败返回false
     */
    public static function createNotice($data = [])
    {
        // 默认参数
        $defaultData = [
            'title' => '',
            'content' => '',
            'describe' => '',
            'user_id' => 0,
            'type' => 0, // 0=文本,1=富文本,3=JSON格式
            'status' => '1', // 1=系统通知,2=阅卷通知,3=会员督学,4=人工答疑
            'read' => 0, // 0=未读,1=已读
            'create_time' => date('Y-m-d H:i:s'),
            'notification_type' => '1' // 1=用户,2=教师端
        ];
        
        // 合并数据
        $insertData = array_merge($defaultData, $data);
        
        // 验证必填字段
        if (empty($insertData['title'])) {
            return false;
        }
        
        try {
            $notice = new self();
            $result = $notice->save($insertData);
            return $result ? $notice->id : false;
        } catch (\Exception $e) {
            return false;
        }
    }
    
    /**
     * 批量创建通知记录
     * @param array $userIds 用户ID数组
     * @param array $noticeData 通知数据
     * @return bool 成功返回true，失败返回false
     */
    public static function createBatchNotice($userIds = [], $noticeData = [])
    {
        if (empty($userIds) || empty($noticeData)) {
            return false;
        }
        $insertData = [];
        $baseData = [
            'title' => $noticeData['title'] ?? '',
            'content' => $noticeData['content'] ?? '',
            'describe' => $noticeData['describe'] ?? '',
            'type' => $noticeData['type'] ?? 0,
            'status' => $noticeData['status'] ?? '1',
            'read' => 0,
            'create_time' => date('Y-m-d H:i:s'),
            'notification_type' => $noticeData['notification_type'] ?? '1'
        ];
        foreach ($userIds as $userId) {
            $insertData[] = array_merge($baseData, ['user_id' => $userId]);
        }
        try {
            $notice = new self();
            return $notice->saveAll($insertData);
        } catch (\Exception $e) {
            return false;
        }
    }
    
    /**
     *  创建会员督学通知的快捷方法
     *  用户ID或用户ID数组
     *  $title 通知标题
     *  $content 通知内容
     *  $describe 描述
     *  成功返回插入的ID或true，失败返回false
     */
    public static function createSupervisionNotice($userIds, $title, $content = '', $describe = '',$arr_content = [])
    {
        $noticeData = [
            'title' => $title,
            'content' => $content,
            'describe' => $describe,
            'status' => '3', // 会员督学
            'notification_type' => '1', // 用户端
            'type' => 3,
            'json_content' => $arr_content ? json_encode($arr_content) : '',
        ];
        if (is_array($userIds)) {
            // 批量创建
            return self::createBatchNotice($userIds, $noticeData);
        } else {
            // 单个创建
            $noticeData['user_id'] = $userIds;
            return self::createNotice($noticeData);
        }
    }
    



}